home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / pxm_ray / pxm_ray.lha / pxm-ray / host_readfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-09  |  8.6 KB  |  338 lines

  1. /*
  2.  * read the input file of the ray-tracer
  3.  */
  4.  
  5. /*
  6.  *    (c) 1988 by George Kyriazis
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    <math.h>
  11. #include    <hypdefs.h>
  12. #include    <hyper.h>
  13. #include    <hypio.h>
  14. #include    <devlib.h>
  15. #include    <devextern.h>
  16. #include    <msgserve.h>
  17.  
  18. #define        HOST
  19. #include    "ray.h"
  20. #include    "msg.h"
  21.  
  22. int    zero = FALSE;
  23. int    one = 4;
  24. int    test;
  25.  
  26. struct    dsp_vector { Ulong x, y, z; };
  27.  
  28. struct    dsp_color { Ulong r,g,b; };
  29.  
  30. struct    dsp_light { struct dsp_vector org; Ulong angle; };
  31.  
  32. struct    dsp_obj    {
  33.         Ushort    type;
  34.         Ushort    dummy;
  35.         union dsp_data {
  36.             struct dsp_sphere {
  37.                 struct    dsp_vector    center;
  38.                 Ulong    radius;
  39.             } sphere;
  40.             struct dsp_quad {
  41.                 struct    dsp_vector    p1;
  42.                 struct    dsp_vector    p2;
  43.                 struct    dsp_vector    p3;
  44.             } quad;
  45.         } data;
  46.         Ulong    reflection;
  47.         Ulong    refraction;
  48.         struct    dsp_color    refl_color;
  49.         struct    dsp_color    refr_color;
  50.         struct    dsp_color    ambient;
  51.         struct    dsp_color    diffuse;
  52.         struct    dsp_color    specular;
  53.         Ulong    width;
  54.         Ulong    index;
  55.         Ulong    refl_diffuse;
  56.         Ulong    refr_diffuse;
  57.         struct    dsp_vector    time;
  58.     };
  59.  
  60. #define    TODSP(x,y)    y = C_FtoDsp(x); y = Swapl(y);
  61.  
  62. readfile(fname)
  63. char    *fname;
  64. {
  65.     FILE    *f;
  66.     int    i, j;
  67.     char    s[10];
  68.  
  69.     f = fopen(fname,"r");
  70.     if(f == NULL) {
  71.         perror("fopen");
  72.         exit(1);
  73.     }
  74.  
  75. /* file format is:
  76.  *    <fov> field of view
  77.  *    <eyex eyey eyez> position of the eye
  78.  *    <dirx diry dirz> direction
  79.  *    <upx upy upz> up vector
  80.  *    <time1 time2> time limits
  81.  *    <foc lens> focusing distance and diameter of lens
  82.  *    <#> background cuing
  83.  *    <min max> minimum and maximum number of interations per pixel
  84.  *    <x y z angle> light source position and angle
  85.  *    number or spheres number of squares
  86.  *     <x y z r [ambient] [diff] [spec] refl r g b refr r g b 
  87.  *           width index refl_diffuse refr_diffuse tx ty tz> 
  88.  *        for every sphere
  89.  *     <x y z x y z x y z [ambient] [diff] [spec]
  90.  *           refl r g b refr r g b width index
  91.  *           refl_diffuse refr_diffuse tx ty tz> 
  92.  *        for every square
  93.  */
  94.  
  95. /* viewing transform */
  96.     fscanf(f, "%lf", &fov);
  97.     fov = tan( fov * M_PI / 180 ) / sqrt(2.0);
  98.     fscanf(f, "%lf %lf %lf", &eye.x, &eye.y, &eye.z);
  99.     fscanf(f, "%lf %lf %lf", &eye_dir.x, &eye_dir.y, &eye_dir.z);
  100.     fscanf(f, "%lf %lf %lf", &up.x, &up.y, &up.z);
  101.  
  102. /* time information */
  103.     fscanf(f, "%lf %lf", &time1, &time2);
  104.  
  105. /* lens information */
  106.     fscanf(f, "%lf %lf", &foc, &lens);
  107.  
  108. /* the background flag */
  109.     fscanf(f, "%s", s);
  110.     if( *s == 'n' ) bgflag = NONE;
  111.     if( *s == 'x' ) bgflag = X;
  112.     if( *s == 'y' ) bgflag = Y;
  113.     if( *s == 'z' ) bgflag = Z;
  114.  
  115. /* how many samples per pixel? */
  116.     fscanf(f, "%d", &tries);
  117.  
  118. /* now the light */
  119.     fscanf(f, "%lf %lf %lf %lf", &light.org.x, &light.org.y,
  120.         &light.org.z, &light.angle);
  121.     light.angle *= M_PI / 180;
  122.  
  123.     fscanf(f, "%d %d", &nos, &nosq);
  124.     noo = nos + nosq;
  125.  
  126.     obj = (struct obj *)malloc(noo * sizeof(struct obj) );
  127.     if(obj == NULL) {
  128.         perror("malloc");
  129.         exit(1);
  130.     }
  131.  
  132.     i = 0;
  133.     for(j = 0; j < nos; j++) {
  134.         obj[i].type = SPHERE;
  135.         fscanf(f, "%lf %lf %lf %lf", &obj[i].data.sphere.center.x,
  136.             &obj[i].data.sphere.center.y,
  137.             &obj[i].data.sphere.center.z,
  138.             &obj[i].data.sphere.radius );
  139.         fscanf(f, "%lf %lf %lf", &obj[i].ambient.r,
  140.             &obj[i].ambient.g,
  141.             &obj[i].ambient.b);
  142.         fscanf(f, "%lf %lf %lf", &obj[i].diffuse.r,
  143.             &obj[i].diffuse.g,
  144.             &obj[i].diffuse.b);
  145.         fscanf(f, "%lf %lf %lf", &obj[i].specular.r,
  146.             &obj[i].specular.g,
  147.             &obj[i].specular.b);
  148.         fscanf(f, "%lf %lf %lf %lf", &obj[i].reflection,
  149.             &obj[i].refl_color.r,
  150.             &obj[i].refl_color.g,
  151.             &obj[i].refl_color.b);
  152.         fscanf(f, "%lf %lf %lf %lf", &obj[i].refraction,
  153.             &obj[i].refr_color.r,
  154.             &obj[i].refr_color.g,
  155.             &obj[i].refr_color.b);
  156.         fscanf(f, "%lf %lf", &obj[i].width, &obj[i].index);
  157.         fscanf(f, "%lf %lf", &obj[i].refl_diffuse,
  158.                 &obj[i].refr_diffuse);
  159.             obj[i].refl_diffuse *= M_PI / 180;
  160.             obj[i].refr_diffuse *= M_PI / 180;
  161.         fscanf(f, "%lf %lf %lf", &obj[i].time.x,
  162.             &obj[i].time.y, &obj[i].time.z);
  163.  
  164.         i++;
  165.     }
  166.  
  167.     for(j = 0 ; j < nosq; j++) {
  168.         obj[i].type = SQUARE;
  169.         fscanf(f, "%lf %lf %lf", &obj[i].data.quad.p1.x,
  170.             &obj[i].data.quad.p1.y,
  171.             &obj[i].data.quad.p1.z);
  172.         fscanf(f, "%lf %lf %lf", &obj[i].data.quad.p2.x,
  173.             &obj[i].data.quad.p2.y,
  174.             &obj[i].data.quad.p2.z);
  175.         fscanf(f, "%lf %lf %lf", &obj[i].data.quad.p3.x,
  176.             &obj[i].data.quad.p3.y,
  177.             &obj[i].data.quad.p3.z);
  178.         fscanf(f, "%lf %lf %lf", &obj[i].ambient.r,
  179.             &obj[i].ambient.g,
  180.             &obj[i].ambient.b);
  181.         fscanf(f, "%lf %lf %lf", &obj[i].diffuse.r,
  182.             &obj[i].diffuse.g,
  183.             &obj[i].diffuse.b);
  184.         fscanf(f, "%lf %lf %lf", &obj[i].specular.r,
  185.             &obj[i].specular.g,
  186.             &obj[i].specular.b);
  187.         fscanf(f, "%lf %lf %lf %lf", &obj[i].reflection,
  188.             &obj[i].refl_color.r,
  189.             &obj[i].refl_color.g,
  190.             &obj[i].refl_color.b);
  191.         fscanf(f, "%lf %lf %lf %lf", &obj[i].refraction,
  192.             &obj[i].refr_color.r,
  193.             &obj[i].refr_color.g,
  194.             &obj[i].refr_color.b);
  195.         fscanf(f, "%lf %lf", &obj[i].width, &obj[i].index);
  196.         fscanf(f, "%lf %lf", &obj[i].refl_diffuse,
  197.                 &obj[i].refr_diffuse);
  198.             obj[i].refl_diffuse *= M_PI / 180;
  199.             obj[i].refr_diffuse *= M_PI / 180;
  200.         fscanf(f, "%lf %lf %lf", &obj[i].time.x,
  201.             &obj[i].time.y, &obj[i].time.z);
  202.  
  203.         i++;
  204.     }
  205.  
  206.     printf("Read data\n");
  207.     send_noo();
  208.     DEVpoll_nodes(DEV_NONE, DEV_NONE,
  209.         0, DEVsystem->draw_nodes - 1,
  210.         0, 63,
  211.         noo,1);
  212.     printf("Begin tracing\n");
  213. }
  214.  
  215. send_noo()
  216. {
  217.     int    i;
  218.     short    no = noo,
  219.         ns = nos,
  220.         nsq = nosq,
  221.         bg = bgflag;
  222.  
  223.     SHORT_SWAP(no);
  224.     SHORT_SWAP(ns);
  225.     SHORT_SWAP(nsq);
  226.     SHORT_SWAP(bg);
  227.     for( i = 0; i < DEVsystem->draw_nodes; i++) {
  228.         HypDrawWrite( DEVsystem->draw_dsp[i], pixel_nos[i],
  229.             &ns, sizeof(short) );
  230.         HypDrawWrite( DEVsystem->draw_dsp[i], pixel_nosq[i],
  231.             &nsq, sizeof(short) );
  232.         HypDrawWrite( DEVsystem->draw_dsp[i], pixel_bgflag[i],
  233.             &bg, sizeof(short) );
  234.         HypDrawWrite( DEVsystem->draw_dsp[i], pixel_status[i],
  235.             &zero, sizeof(int) );
  236.     }
  237.  
  238. }
  239.  
  240. send_light(l, node)
  241. int    node;
  242. Ushort    l;
  243. {
  244.     struct    dsp_light    dsp_l;
  245.  
  246.     dsp_l.org.x = C_FtoDsp(light.org.x); dsp_l.org.x = Swapl(dsp_l.org.x);
  247.     dsp_l.org.y = C_FtoDsp(light.org.y); dsp_l.org.y = Swapl(dsp_l.org.y);
  248.     dsp_l.org.z = C_FtoDsp(light.org.z); dsp_l.org.z = Swapl(dsp_l.org.z);
  249.     dsp_l.angle = C_FtoDsp(light.angle); dsp_l.angle = Swapl(dsp_l.angle);
  250.     HypDrawWrite( DEVsystem->draw_dsp[node], l,
  251.         &dsp_l, sizeof( struct dsp_light ) );
  252. }
  253.  
  254. send_object(i, o, node)
  255. Ushort    i;
  256. Ushort    o;
  257. int    node;
  258. {
  259.     struct    dsp_obj    dsp_o;
  260.  
  261.     dsp_o.type = (Ushort) obj[i].type;
  262.     SHORT_SWAP( dsp_o.type );
  263.     switch( obj[i].type ) {
  264.         case SPHERE:
  265.             TODSP( obj[i].data.sphere.center.x, 
  266.                 dsp_o.data.sphere.center.x);
  267.             TODSP( obj[i].data.sphere.center.y, 
  268.                 dsp_o.data.sphere.center.y);
  269.             TODSP( obj[i].data.sphere.center.z, 
  270.                 dsp_o.data.sphere.center.z);
  271.             TODSP( obj[i].data.sphere.radius, 
  272.                 dsp_o.data.sphere.radius);
  273.             break;
  274.         case SQUARE:
  275.             TODSP( obj[i].data.quad.p1.x, dsp_o.data.quad.p1.x );
  276.             TODSP( obj[i].data.quad.p1.y, dsp_o.data.quad.p1.y );
  277.             TODSP( obj[i].data.quad.p1.z, dsp_o.data.quad.p1.z );
  278.             TODSP( obj[i].data.quad.p2.x, dsp_o.data.quad.p2.x );
  279.             TODSP( obj[i].data.quad.p2.y, dsp_o.data.quad.p2.y );
  280.             TODSP( obj[i].data.quad.p2.z, dsp_o.data.quad.p2.z );
  281.             TODSP( obj[i].data.quad.p3.x, dsp_o.data.quad.p3.x );
  282.             TODSP( obj[i].data.quad.p3.y, dsp_o.data.quad.p3.y );
  283.             TODSP( obj[i].data.quad.p3.z, dsp_o.data.quad.p3.z );
  284.             break;
  285.         default:
  286.             break;
  287.     }
  288.     TODSP( obj[i].reflection, dsp_o.reflection);
  289.     TODSP( obj[i].refraction, dsp_o.refraction);
  290.     TODSP( obj[i].refl_color.r, dsp_o.refl_color.r);
  291.     TODSP( obj[i].refl_color.g, dsp_o.refl_color.g);
  292.     TODSP( obj[i].refl_color.b, dsp_o.refl_color.b);
  293.     TODSP( obj[i].refr_color.r, dsp_o.refr_color.r);
  294.     TODSP( obj[i].refr_color.g, dsp_o.refr_color.g);
  295.     TODSP( obj[i].refr_color.b, dsp_o.refr_color.b);
  296.     TODSP( obj[i].ambient.r, dsp_o.ambient.r);
  297.     TODSP( obj[i].ambient.g, dsp_o.ambient.g);
  298.     TODSP( obj[i].ambient.b, dsp_o.ambient.b);
  299.     TODSP( obj[i].diffuse.r, dsp_o.diffuse.r);
  300.     TODSP( obj[i].diffuse.g, dsp_o.diffuse.g);
  301.     TODSP( obj[i].diffuse.b, dsp_o.diffuse.b);
  302.     TODSP( obj[i].specular.r, dsp_o.specular.r);
  303.     TODSP( obj[i].specular.g, dsp_o.specular.g);
  304.     TODSP( obj[i].specular.b, dsp_o.specular.b);
  305.     TODSP( obj[i].width, dsp_o.width);
  306.     TODSP( obj[i].index, dsp_o.index);
  307.     TODSP( obj[i].refl_diffuse, dsp_o.refl_diffuse);
  308.     TODSP( obj[i].refr_diffuse, dsp_o.refr_diffuse);
  309.     TODSP( obj[i].time.x, dsp_o.time.x);
  310.     TODSP( obj[i].time.y, dsp_o.time.y);
  311.     TODSP( obj[i].time.z, dsp_o.time.z);
  312.  
  313.     HypDrawWrite( DEVsystem->draw_dsp[node], o,
  314.         &dsp_o, sizeof( struct dsp_obj ));
  315. }
  316.  
  317. send_data(opcode, dsp, node)
  318. int    opcode;
  319. DrawDsp    *dsp;
  320. int    node;
  321. {
  322.     Ushort    o;    /* DSP obj address */
  323.     Ushort    l;    /* DSP light address */
  324.     Ushort    i;
  325.  
  326.     i = HypDrawGetPir(dsp);
  327. /* get the object pointer address */
  328.     o = HypDrawGetPir(dsp);
  329. /* now get the light address */
  330.     l = HypDrawGetPir(dsp);
  331.  
  332.     send_light(l, node);
  333.     send_object(i, o, node);
  334.  
  335.     HypDrawWrite( DEVsystem->draw_dsp[node], pixel_status[node],
  336.         &zero, sizeof(int) );
  337. }
  338.